package com.engine.jucailinkq.common.util;

import com.google.common.collect.Maps;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DbTools {

    public static ThreadLocal<RecordSet> thread = ThreadLocal.withInitial(()->new RecordSet());

    public static ThreadLocal<RecordSetTrans> threadRecordSetTrans = ThreadLocal.withInitial(()->new RecordSetTrans());

    public static List<Map<String,Object>> getSqlToList(String sql,Object...value){
        RecordSet rs = thread.get();

        List<Map<String,Object>> result = new ArrayList<>();
        if (value == null || value.length <=0 || "".equals(value)){
            rs.executeQuery(sql);
        }else {
            rs.executeQuery(sql,value);
        }
        while (rs.next()){
            Map<String,Object> dataMap = new HashMap<>();
            String[] columns = rs.getColumnName();
            for (String column:columns){
                dataMap.put(column.toLowerCase(),rs.getString(column));
                if (column.toLowerCase().equals("keyid")){
                    dataMap.put("key",rs.getString(column));
                }
            }
            result.add(dataMap);
        }
        return result;
    }

    public static List<Map<String,Object>> getSqlToListKeySmallLetter(String sql,Object...value){
        RecordSet rs = thread.get();

        List<Map<String,Object>> result = new ArrayList<>();
        if (value == null || value.length <=0 || "".equals(value)){
            rs.executeQuery(sql);
        }else {
            rs.executeQuery(sql,value);
        }
        while (rs.next()){
            Map<String,Object> dataMap = new HashMap<>();
            String[] columns = rs.getColumnName();
            for (String column:columns){
                dataMap.put(column.toLowerCase(),rs.getString(column));
            }
            result.add(dataMap);
        }
        return result;
    }

//    public static List<Map<String,Object>> getSqlToList(String sql,String dataSourceName){
//        RecordSetDataSource rs = threads.get();
//        if (rs == null){
//            rs = new RecordSetDataSource(dataSourceName);
//            threads.set(rs);
//        }
//        List<Map<String,Object>> result = new ArrayList<>();
//        rs.execute(sql);
//        while (rs.next()){
//            Map<String,Object> dataMap = new HashMap<>();
//            String[] columns = rs.getColumnName();
//            for (String column:columns){
//                dataMap.put(column,rs.getString(column));
//            }
//            result.add(dataMap);
//        }
//        return result;
//    }



    public static Map<String,Object> getSqlToMap(String sql,Object...value){
        RecordSet rs = thread.get();
        Map<String,Object> result = Maps.newHashMap();
        if (value == null || value.length <=0 || "".equals(value)){
            rs.executeQuery(sql);
        }else {
            rs.executeQuery(sql,value);
        }
        if (rs.next()){
            String[] columns = rs.getColumnName();
            for (String column:columns){
                result.put(column.toLowerCase(),rs.getString(column));
            }
        }
        return result;
    }


    public static Map<String,Object> getSqlToMapList(String sql,Object...value){
        RecordSet rs = thread.get();
        Map<String,Object> result = Maps.newHashMap();
        if (value == null || value.length <=0 || "".equals(value)){
            rs.executeQuery(sql);
        }else {
            rs.executeQuery(sql,value);
        }
        while (rs.next()){
            result.put(rs.getString("id"),rs.getString("name"));
        }
        return result;
    }



    public static boolean update(String sql,Object...value){
        RecordSet rs = thread.get();
        if (value == null || value.length <=0 || "".equals(value)){
            return rs.executeUpdate(sql);
            
        }else {
            return rs.executeUpdate(sql,value);
        }
    }

    public static boolean update(RecordSet rs,String sql,Object...value){
        if (value == null || value.length <=0 || "".equals(value)){
            return rs.executeUpdate(sql);

        }else {
            return rs.executeUpdate(sql,value);
        }
    }

    /**
     * 获取sql语句
     * @param sql
     * @param value
     * @return
     */
    public  static  String getSql(String sql,Object...value){
        RecordSet rs = thread.get();
        rs.executeQuery(sql,value);
        String sentenceSql = "";
        if (rs.next()){
            sentenceSql = rs.getString("sentence");
        }
        return sentenceSql;
    }
    public static boolean updateBatch(String sql,List<List> dataList){
        RecordSet rs = thread.get();
        return rs.executeBatchSql(sql,dataList);
    }

    public static Map<String,String> getSimpleNameMap(String sql,Object...value){
        RecordSet rs = thread.get();
        Map<String,String> result = Maps.newHashMap();
        if (value == null || value.length <=0 || "".equals(value)){
            rs.executeQuery(sql);
        }else {
            rs.executeQuery(sql,value);
        }
        while (rs.next()){
            result.put(rs.getString("id"),rs.getString("name")+"&"+rs.getString("simplename")+"&"+rs.getString("ys"));
        }
        return result;
    }

}
